package com.baizhi.action;

import com.opensymphony.xwork2.Action;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import java.io.File;

public class FileUploadAction implements Action {
    //接收数据
    private File upload;
    //接收文件的原始名称  文件名.后缀   上传文件+"FileName"
    private String uploadFileName;
    //获取上传文件的类型         上传文件+"ContentType" 文件类型    MIME类型  大类型/小类型   text/html
    private String uploadContentType;
    private String realPath;

    @Override
    public String execute() throws Exception {
        System.out.println("file name is " +uploadFileName);
        System.out.println("file type is "+uploadContentType);
        //调用业务    读写文件
        //通过相对路径  获取  绝对路径(当前upload文件夹在服务器上的绝对路径)
        //ServletContext.getRealPath("相对路径")
        String path = ServletActionContext.getRequest().getSession().getServletContext().getRealPath(realPath);
        FileUtils.copyFile(upload , new File(path+"\\"+uploadFileName));
        //跳转页面
        return "success";
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getRealPath() {
        return realPath;
    }

    public void setRealPath(String realPath) {
        this.realPath = realPath;
    }
}
